<?php


/**
 * 套餐管理
 * Enter description here ...
 * @author Admin
 *
 */
class PackageAction extends CommonAction{
	
	
	/**
	 * 列表
	 * Enter description here ...
	 */
	public function index() {
		$package = D('Package');
		$packagedata = $package->findAll();
		
		$this->assign("packagedata",$packagedata);
		$this->display();
	}
	
	
	/**
	 * 新建套餐保存
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$data = Init_GP(array('amount','beat','pointb'));   //货币金额
		
		$package = D('Package');  //套餐
		$accessory = A('Accessory');   //附件管理Action
			
		if(empty($_FILES)){   //
			$this->error('必须选择上传文件');
			
		}else{
			$a = $accessory->upFile('Package');  //先上传文件返回结果信息
			if(isset($a)){  
				$data['path']=$a[0]['savename'];   //保存文件名称
				
				$data = $package->create($data);   
				if(empty($data) || $data === false) {
					$this->error($package->getError());
				}
				
				$id = $package->add($data);  //保存套餐
				
				
				if($id){ //保存成功
					$this->assign('jumpUrl','__URL__/index');
					$this->success("添加成功");
				}else { //失败提示
					$this->error($activity->getError());
				}
			}else{
				$this->error('上传文件有异常请与系统管理员联系');
			}
		}

	}
	
	
	/**
	 * 修改套餐
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$id = GetNum($_GET['id']);  //主键
		$package = D('Package');  //套餐
		$data = $package->find($id);
				
		$this->assign('data',$data);
		
		$this->display();
	}
	
	
	/**
	 * 修改确认
	 * @see CommonAction::update()
	 */
	public function update(){
		$data = Init_GP(array('amount','beat','pointb'));
		$id = GetNum($_POST['id']);  //主键ID
		
		$package = D('Package'); 
		$accessory = A('Accessory');
		
		/**
		 * 保证每条记录都有图片
		 * 修改：修改的记录已经没有图片了则必须上传图片，如果还存在图片则没有必要强制要图片
		 */
		
		$obj = $package->where("id=$id")->Field("path")->select(); 
		$imageName = $obj[0]['path'];
		
		Log::write("..图片路径..:" . $imageName,Log::DEBUG);
		
		if(!empty($imageName)){
			Log::write("..修改记录存在图片!..:" ,Log::DEBUG);
			
			$obj["amount"] = $data["amount"];	
			$obj["beat"] = $data["beat"];	
			$obj["pointb"] = $data["pointb"];	
				
			$st = $package ->where("id=$id")->save($obj);  
			
			if($st){
				$this->assign('jumpUrl','__URL__/index');    
				$this->success("修改成功");
				
			}else{
				$this->error('修改失败！');
			}
			
		}else{
			Log::write("..修改记录不存在图片!..:" ,Log::DEBUG);
			
				if(empty($_FILES)){   // 判断是否存在图片
					$this->error('必须选择上传文件');
				}else{
					$a=$accessory->upFile('Package');  //先上传文件返回结果信息
					
					if(isset($a)){  
						$obj['path']=$a[0]['savename'];   //保存文件名称
						
						$obj["amount"] = $data["amount"];	
						$obj["beat"] = $data["beat"];	
						$obj["pointb"] = $data["pointb"];	
						
						
						$st = $package ->where("id=$id")->save($obj);  //更新记录	
						
						if($st){ //修改成功
							$this->assign('jumpUrl','__URL__/index');
							$this->success("修改成功");
						}else {  //失败提示
							$this->error('修改失败！');
						}
					}else{
						$this->error('上传文件有异常请与系统管理员联系');
					}
				}
				
			
		}
		
		
	}
	
	
	
	/**
	 * 删除（单条）
	 * @see CommonAction::del()
	 */
	public function del(){
		$id = GetNum($_GET['id']);  //主键
		//Log::write("...删除单条..:" . $id);
		
		$package = D('Package'); 
		
		$obj = $package->where("id=$id")->Field("path")->select();   //获取数组形式
		
		$imageName = $obj[0]['path'];
		
		$fullpath = UPLOADIMGEEXTEND.'Package/' .$imageName ;
		
		$st = $package->where("id=$id")->delete();
		if($st){
			
			@unlink($fullpath);    //删除图片附件
			
			$this->assign('jumpUrl','__URL__/index');     //跳转到列表首页
			$this->success("删除成功");
			
		}else{
			$this->error('删除失败！');
		}
		
	}
	
	
	
	
	/**
	 * 删除（多条）
	 * @see CommonAction::del()
	 */
	public function dels(){
		$data = Init_GP(array('ids'));    //参数
		$ids = $data['ids'];  //多个主键
		
		$package = D('Package');
		
		$prefix = C('DB_PREFIX');   //数据库前缀
		 if(!empty($package)) {
            $ids       = getdotstring($ids,'int');  //字符串分割
            
            //Log::write("...删除多条..:" . $ids);
            	
            $objs = $package->where("id in ($ids)")->Field("path")->select();  //现获取删除记录上图片
            	
            $sql = "DELETE U FROM `{$prefix}package` as U  WHERE U.id in({$ids})";   //删除多条记录
            $st = $package->execute($sql) ;
            
            if ($st){

            	if(!empty($objs)){  //循环数组删除图片
            		foreach($objs as $key => $value){
            			$fullpath = null ;
            			$fullpath = UPLOADIMGEEXTEND.'Package/' .$value['path'] ;
            			
            			@unlink($fullpath);    //删除图片附件
            		}
            	}
            	
            	$this->success('已删除选定项');
            }else{
            	$this->error($activity->getError());
            }
        }
		
	}
	
	
	
	/**
	 * 删除图片
	 * Enter description here ...
	 */
	public function delImg(){
		$id = GetNum($_GET['id']);   //主键
		
		if(!empty($id)){
			$model = M('Package');
			
			$obj = $model->find($id);  //获取对象
			$imageName = $obj['path'];   //图片  
			
			$obj['path'] = '';
			$st = $model->save($obj);    //置空图片
			
			if(!empty($st)){   
				//...更新记录成功后删除物理文件
				$fullpath = UPLOADIMGEEXTEND.'Package/' .$imageName ;
				
				@unlink($fullpath);    //删除文件
				
				$data['state']= "success";
				echo json_encode($data);
			}else {
				$data['state']= "error";
				echo json_encode($data);
			}
		}
	}
	
	
	
	
	
	
}
?>